<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// 接口（后台）
Route::match(
    ['post', 'get',],
    '/backend/{controller?}/{action?}',
    function($controller = 'frontend', $action = 'index') {
        $request = request();
        $ip      = $request->ip();
        try{
            $post = $request->all();
            // 控制器   中划线 => 驼峰式:大小写
            $controller = ucfirst($controller);
            if($controller == 'Db'){
                $file = 'App\Http\Controllers\Test\DatabaseController';
            } else{
                $file = 'App\Http\Controllers\Backend\\'.$controller.'Controller';
            }

            $file   = str_replace(' ', '', ucwords(str_replace('-', ' ', $file)));
            $action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));
            $action = lcfirst($action);

            //var_dump($file);die;
            // 执行请求
            $file   = App::make($file);
            $result = $file->$action($post);

            return $result;
        } catch(RuntimeException $e){
            if($ip == '127.0.0.1'){
                return ['code' => $e->getCode(), 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()];
            } else{
                return ['code' => $e->getCode(), 'msg' => $e->getMessage(), 'line' => $e->getLine()];
            }
        }
    }
)->where(['controller' => '[0-9a-zA-Z-]+', 'action' => '[0-9a-zA-Z-]+'])->name('backend'); // 统一命令


// 接口（后台）
/*
Route::prefix('backend')->match(
    ['post', 'get',],
    '/{controller?}/{action?}',
    function($controller = 'frontend', $action = 'index') {
        try{
            $request = request();
            $post    = $request->all();

            // 控制器   中划线 => 驼峰式:大小写
            if($controller == 'db'){
                $file = 'App\Http\Controllers\DatabaseController';
            } else{
                $file = 'App\Http\Controllers\Backend\\'.$controller.'Controller';
            }

            $file   = str_replace(' ', '', ucwords(str_replace('-', ' ', $file)));
            $action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));

            //var_dump($file);die;
            // 执行请求
            $file   = App::make($file);
            $result = $file->$action($post);

            return $result;
        } catch(Exception $e){
            return ['code' => 1, 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()];
        }
    }
)->where(['controller' => '[0-9a-zA-Z-]+', 'action' => '[0-9a-zA-Z-]+'])->name('backend'); // 统一命令*/


/*Route::match(
    ['get', 'post'],
    '/{module?}/{controller?}/{action?}',
    function($module = 'Curl', $controller = 'frontend', $action = 'index') {
        try{
            $request = request();
            $post    = $request->all();

            // 控制器   中划线 => 驼峰式:大小写
            $file   = 'App\Http\Controllers\\'.$module.'\\'.$controller.'Controller';
            $file   = str_replace(' ', '', ucwords(str_replace('-', ' ', $file)));
            $action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));

            // 执行请求
            $file   = App::make($file);
            $result = $file->$action($post);

            return $result;
            // 如果不是主页 生成模板页面
            //$msg    = $controller->getMsg();
            //$msg    = $msg ? $msg : '操作成功！！';
            //$toView = ['shows' => $result, 'toPath' => $path, 'api_type' => 'doc',];
            //$toView = ['code' => 200, 'msg' => $msg, 'data' => $result];
            //return response()->json($toView);

            //return view('./doctest/document', $toView);

        } catch(Exception $e){
            return ['code' => 1, 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()];
        }
    }
)->where(['module' => '[0-9a-zA-Z-]+', 'controller' => '[0-9a-zA-Z-]+', 'action' => '[0-9a-zA-Z-]+'])->name('curl'); // 统一命令
*/
